$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sjdriver/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '用户名', name: 'username', index: 'username', width: 80 },
            { label: '真实姓名', name: 'realname', index: 'realname', width: 50 },
			// { label: '密码', name: 'password', index: 'password', width: 80 },
			// { label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
            { label: '性别', name: 'sex', index: 'sex', width: 30,
                formatter:function(v){
                    if(v==0){
                        return "女";
                    }else if(v==1){
                        return "男";
                    }else{
                        return "";
                    }
                }
            },
            { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
            { label: '年龄', name: 'age', index: 'age', width: 30 },
			{ label: '身份证号', name: 'idCard', index: 'id_card', width: 100 },
			// { label: '头像路径', name: 'avatar', index: 'avatar', width: 80 },
            { label: '车型', name: 'carstyle', index: 'carstyle', width: 70 },
            { label: '车牌号', name: 'carno', index: 'carno', width: 70 },
			{ label: '邮箱', name: 'email', index: 'email', width: 80 ,hidden:true},
			{ label: '驾驶状态', name: 'driveStatus', index: 'drive_status', width: 60 ,
                formatter:function(v){
                    if(v==0){
                        return "未出车";
                    }else if(v==1){
                        return "出车";
                    }else if(v==2){
                        return "订单进行中";
                    }
                }
			},
			{ label: '高德云图id', name: 'gaoId', index: 'gao_id', width: 80 ,hidden:true},
			{ label: '成为司机时间', name: 'createTime', index: 'create_time', width: 120 },
			// { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 },
            { label: '极光注册ID', name: 'registrationId', index: 'registration_id', width: 100 },
			{ label: '是否可用', name: 'status', index: 'status', width: 50 ,
                cellattr:function(rowId, val, rawObject, cm, rdata) {
                    if(rawObject.ifExamine==0) {
                        return "style='color:red;font-weight:bold'";
                    }
                },
                formatter:function(v){
                    if(v==0){
                        return "不可用";
                    }else if(v==1){
                        return "可用";
                    }else{
                    	return "";
					}
                }
			}

        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		q:{
            realname:null,
			mobile:null,
            idCard:null
		},
		sjDriver: {
			updateTime:null
		},
        updateDate:null,
        imageUrl: ''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sjDriver = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            if(vm.sjDriver.updateTime != null && vm.sjDriver.updateTime != vm.updateDate) {
                vm.sjDriver.updateTime = dateChange(vm.sjDriver.updateTime);
            }
            if(vm.sjDriver.driveStatus == "未出车"){
                vm.sjDriver.driveStatus = 0;
            }else if(vm.sjDriver.driveStatus == "出车"){
                vm.sjDriver.driveStatus = 1;
            }else if(vm.sjDriver.driveStatus == "订单进行中"){
                vm.sjDriver.driveStatus = 2;
            }else{
                vm.sjDriver.driveStatus = "";
            }
			var url = vm.sjDriver.id == null ? "sjdriver/save" : "sjdriver/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.sjDriver),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sjdriver/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sjdriver/info/"+id, function(r){
                vm.sjDriver = r.sjDriver;
                if(vm.sjDriver.driveStatus == 0){
                    vm.sjDriver.driveStatus = "未出车";
				}else if(vm.sjDriver.driveStatus == 1){
                    vm.sjDriver.driveStatus = "出车";
				}else if(vm.sjDriver.driveStatus == 2){
                    vm.sjDriver.driveStatus = "订单进行中";
                }else{
                    vm.sjDriver.driveStatus = "";
				}
                vm.updateDate = vm.sjDriver.updateTime;

                var frontUrl = '/qwj_shop/fileDirect/backManageDriver/';
                if(vm.sjDriver.avatar == null){
                    vm.imageUrl = "../../swagger/noPicture.jpg"
                }else {
                    vm.imageUrl = frontUrl + vm.sjDriver.avatar;
                }
            });
		},
        handleAvatarSuccess: function(res, file) {
            vm.sjDriver.avatar = res.url;
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload: function(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                alert('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                alert('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'realname': vm.q.realname, 'mobile':vm.q.mobile, 'idCard':vm.q.idCard},
                page:page
            }).trigger("reloadGrid");
		}
	}
});

function dateChange(dateTime){
    if(dateTime != null) {
        var seperator1 = "-";
        var year = dateTime.getFullYear();
        var month = dateTime.getMonth() + 1;
        var strDate = dateTime.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }else{
        return null;
    }
};